package com.zprmb.udzt.fragment;

import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.LoginActivity;
import com.zprmb.udzt.R;
import com.zprmb.udzt.adapter.DexterityBillAdapter;
import com.zprmb.udzt.bean.DexterityBill_Bean;
import com.zprmb.udzt.bean.V4InfoBean;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;

public class DexterityBillFragment extends BaseFragment {

	private V4InfoBean info;
	private PullToRefreshListView lv_v4_dexteritybull_fragment;
	private ListView actualListView;
	private boolean isyou = true;
	private int page = 2;
	private ArrayList<DexterityBill_Bean> dateOfadapter = new ArrayList<DexterityBill_Bean>();
	private DexterityBillAdapter adapter;
	private String status;
	private TextView tv_v4_dexteritybill_1;
	private TextView tv_v4_dexteritybill_2;
	private TextView tv_v4_dexteritybill_3;
	private TextView tv_v4_dexteritybill_4;	
	private LinearLayout ll_v4_dexteritybill_goto;
	private TextView tv_v4_dexteritybill_goto;
	
	
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:// 正常
				if (info.dbb.size() < 1) {
					lv_v4_dexteritybull_fragment.setVisibility(View.GONE);
					ll_v4_dexteritybill_goto.setVisibility(View.VISIBLE);
				} else {
					ll_v4_dexteritybill_goto.setVisibility(View.GONE);
					lv_v4_dexteritybull_fragment.setVisibility(View.VISIBLE);
					dateOfadapter.addAll(info.dbb);
					adapter = new DexterityBillAdapter(getActivity(), dateOfadapter, status);
					actualListView.setAdapter(adapter);
					lv_v4_dexteritybull_fragment.onRefreshComplete();
				}
				break;
			case 2:// 下拉
				isyou = true;
				dateOfadapter.clear();
				dateOfadapter.addAll(info.dbb);
				adapter = new DexterityBillAdapter(getActivity(),dateOfadapter,status);
				actualListView.setAdapter(adapter);
				lv_v4_dexteritybull_fragment.onRefreshComplete();
				break;
			case 3:// 上拉
				if (info.dbb.size() < 1) {
					isyou = false;
				}
				dateOfadapter.addAll(info.dbb);
				adapter.notifyDataSetChanged();
				lv_v4_dexteritybull_fragment.onRefreshComplete();
				break;
			}
		};
	};
	
	public static DexterityBillFragment newInstance(int position, String string) {
		DexterityBillFragment db = new DexterityBillFragment();
		Bundle b = new Bundle();
		b.putString("status", string);
		db.setArguments(b);
		return db;
	}

	public void onAttach(android.app.Activity activity) {
		super.onAttach(activity);
	};
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		status = getArguments().getString("status");
	}
	
	public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.v4_dexteritybill_fragment,
				container, false);
		
		lv_v4_dexteritybull_fragment = (PullToRefreshListView) view.findViewById(R.id.lv_v4_dexteritybull_fragment);
		actualListView = lv_v4_dexteritybull_fragment.getRefreshableView();
		registerForContextMenu(actualListView);
		
		ll_v4_dexteritybill_goto = (LinearLayout) view.findViewById(R.id.ll_v4_dexteritybill_goto);
		tv_v4_dexteritybill_goto = (TextView) view.findViewById(R.id.tv_v4_dexteritybill_goto);
		
		tv_v4_dexteritybill_goto.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				getActivity().finish();
			}
		});
		
		lv_v4_dexteritybull_fragment.setOnRefreshListener(new OnRefreshListener<ListView>() {
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				page = 2;
				initData(1, 2,false);
			}
		});
		
		lv_v4_dexteritybull_fragment.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			public void onLastItemVisible() {
				if (isyou) {
					initData(page, 3,false);
					page++;
				}
			}
		});
		
		tv_v4_dexteritybill_1 = (TextView) view.findViewById(R.id.tv_v4_dexteritybill_1);
		tv_v4_dexteritybill_2 = (TextView) view.findViewById(R.id.tv_v4_dexteritybill_2);
		tv_v4_dexteritybill_3 = (TextView) view.findViewById(R.id.tv_v4_dexteritybill_3);
		tv_v4_dexteritybill_4 = (TextView) view.findViewById(R.id.tv_v4_dexteritybill_4);
		tv_v4_dexteritybill_1.getPaint().setFakeBoldText(true);
		tv_v4_dexteritybill_2.getPaint().setFakeBoldText(true);
		tv_v4_dexteritybill_3.getPaint().setFakeBoldText(true);
		tv_v4_dexteritybill_4.getPaint().setFakeBoldText(true);
		
		if (status.equals("IN")) {
			tv_v4_dexteritybill_1.setText("日期");
			tv_v4_dexteritybill_2.setText("转入金额(元)");
			tv_v4_dexteritybill_3.setText("状态");
			tv_v4_dexteritybill_4.setVisibility(View.GONE);
		}else {
			tv_v4_dexteritybill_1.setText("日期");
			tv_v4_dexteritybill_2.setText("转出金额(元)");
			tv_v4_dexteritybill_3.setText("手续费(元)");
			tv_v4_dexteritybill_4.setVisibility(View.VISIBLE);
			tv_v4_dexteritybill_4.setText("到账金额(元)");
		}
		return view;
	}
	
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		info = new V4InfoBean();
		initData(1, 1,true);
	}

	private void initData(int page, final int handlerId,boolean isShow) {
		AjaxParams params = new AjaxParams();
		params.put("pageNo", page+"");
		params.put("pageSize", "12");
		
		VolleryUtil vollery = new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if (jsonObject.optInt("success")==1) {
					Log.e(status, jsonObject.toString());
					JSONObject json = jsonObject.optJSONObject("data");
					info.parseJSON_dexteritybill(json.optJSONArray("resultList"));
					handler.sendEmptyMessage(handlerId);
				}else {
					if (jsonObject.optString("comment").equals("please login")) {
						Intent intent = new Intent(getActivity(),LoginActivity.class);
						startActivity(intent);
					}
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		vollery.setHeaders("token", getActivity().getSharedPreferences("config", 0).getString("token", ""));
		vollery.setHeaders("client", "android");
		vollery.setRequestMethod(0);
		vollery.setShow(isShow);
		if (status.equals("IN")) {
			vollery.execute(getActivity(), Constant.URL+"api/v2/current/user/inRecord?", params.toString());
		}else {
			vollery.execute(getActivity(), Constant.URL+"api/v2/current/user/outRecord?", params.toString());
		}
	}
}
